#include "include.h"
#include "bsp_effect_set_in_uart.h"

/*
 * 文件名称: bsp_effect_set_in_uart.c
 * 功能描述: 本文件为无线麦在线调试音效相关处理
             功能使用需要安装 PC上位机 以及 串口通信模块

            目前暂时只能使用无线麦项目的调试  后续再做多平台兼容性
 ****************************************************************************************
    code :
    buf  :
    time :
 */
#if EFFECT_SET_IN_UART
extern u8 eq_rx_buf[EQ_BUFFER_LEN];
u8 dsp_tx_ack_buf[14];//AT(.buf.online_effect);

AT(.text.online_effect)
void dsp_set_mic_effect(void)
{
//    for(int q = 0;q < 10; q++){
//        printf("%c", eq_rx_buf[q]);
//    }
//    printf("\n");
    if(wireless_cb.alg_en){
        if (0 == memcmp((char *)&eq_rx_buf[0], "LEMIC_ECHO", 10)) {
            printf("LEMIC_ECHO %d\n",(little_endian_read_16(eq_rx_buf, XTP_DSP_FRAME_HEAD_TAG)&0xffff) - XTP_DSP_FRAME_SIZE_LEN);
            printf("Lp_filter_en %d attenuation%d freq%d\n",eq_rx_buf[12],eq_rx_buf[13],little_endian_read_16(eq_rx_buf, 16)&0xffff);
            printf("dry %d wet%d delay%d\n",little_endian_read_16(eq_rx_buf, 18)&0xffff,little_endian_read_16(eq_rx_buf, 20)&0xffff,little_endian_read_16(eq_rx_buf, 14)&0xffff);
    #if ECHO_EN
            echo_audio_param_set(eq_rx_buf[13],little_endian_read_16(eq_rx_buf, 14)&0xffff,little_endian_read_16(eq_rx_buf, 16)&0xffff,
                                 eq_rx_buf[12],little_endian_read_16(eq_rx_buf, 18)&0xffff,little_endian_read_16(eq_rx_buf, 20)&0xffff);
    #endif
        }else if(0 == memcmp((char *)&eq_rx_buf[0], "LEMIC_PITC", 10)){
    //        printf("LEMIC_PITC %d\n",(little_endian_read_16(eq_rx_buf, XTP_DSP_FRAME_HEAD_TAG)&0xffff) - XTP_DSP_FRAME_SIZE_LEN);
            s16 shift = little_endian_read_16(eq_rx_buf, XTP_DSP_FRAME_CH_ADDR)&0xffff;
            printf("shift %d\n",shift);
    #if MAGIC_EN
            magic_audio_param_set(shift);
    #endif
        }
    }
}

AT(.text.online_effect)
void bsp_online_set_lemic_effect(void)
{
    u8 dsp_channel_set = DSP_CHANNEL_ERROR;
    u16 size;
    if (0 == memcmp((char *)&eq_rx_buf[0], "LEMIC", 5)) {
        dsp_channel_set = DSP_CHANNEL_LEMIC;
    }

    memset(dsp_tx_ack_buf, 0, 14);
    memcpy(dsp_tx_ack_buf, eq_rx_buf, 10);
    size = little_endian_read_16(eq_rx_buf, XTP_DSP_FRAME_SIZE_ADDR);
    u32 crc = calc_crc(eq_rx_buf, size+10, EQ_CRC_SEED);
    if (crc == little_endian_read_16(eq_rx_buf, XTP_DSP_FRAME_HEAD_TAG+size)) {
        dsp_effect_tx_ack(dsp_tx_ack_buf, 0);
    } else {
        dsp_effect_tx_ack(dsp_tx_ack_buf, 1);
        printf("-->CRC_ERROR\n");
        return;
    }

    if (dsp_channel_set == DSP_CHANNEL_LEMIC) {
        dsp_set_mic_effect();
    } else {
        dsp_effect_tx_ack(dsp_tx_ack_buf, 1);
        memset(eq_rx_buf, 0, EQ_BUFFER_LEN);
        printf("!!!dsp_channel_set error\n");
        return;
    }
    dsp_effect_tx_ack(dsp_tx_ack_buf, 0);
    memset(eq_rx_buf, 0, EQ_BUFFER_LEN);
}

#endif
